<?php
class EventsController extends AppController {

	var $name = 'Events';

	function index() {
		$user = $this->Session->read('Auth.User');
		if($user['user_role']=='admin'){
			$this->layout = 'admin';
			$this->Event->recursive = 0;
			$this->set('events', $this->paginate());
		} else {
			$this->Session->setFlash(__('Bu sayfaya erişmeye yetkiniz yoktur!', true));
			$this->redirect(array('controller'=>'pages','action' => 'index'));
		}
	}

	function view($id = null) {
		$user = $this->Session->read('Auth.User');
		if($user['user_role']=='admin'){
			$this->layout = 'admin';
			if (!$id) {
				$this->Session->setFlash(__('Invalid event', true));
				$this->redirect(array('action' => 'index'));
			}
			$this->set('event', $this->Event->read(null, $id));
		} else {
			$this->Session->setFlash(__('Bu sayfaya erişmeye yetkiniz yoktur!', true));
			$this->redirect(array('controller'=>'pages','action' => 'index'));
		}
	}

	function add() {
		$user = $this->Session->read('Auth.User');
		if($user['user_role']=='admin'){
			$this->layout = 'admin';
			if (!empty($this->data)) {
				$this->Event->create();
				if ($this->Event->save($this->data)) {
					$this->Session->setFlash(__('The event has been saved', true));
					$this->redirect(array('action' => 'index'));
				} else {
					$this->Session->setFlash(__('The event could not be saved. Please, try again.', true));
				}
			}
		} else {
			$this->Session->setFlash(__('Bu sayfaya erişmeye yetkiniz yoktur!', true));
			$this->redirect(array('controller'=>'pages','action' => 'index'));
		}
	}

	function edit($id = null) {
		$user = $this->Session->read('Auth.User');
		if($user['user_role']=='admin'){
			$this->layout = 'admin';
			if (!$id && empty($this->data)) {
				$this->Session->setFlash(__('Invalid event', true));
				$this->redirect(array('action' => 'index'));
			}
			if (!empty($this->data)) {
				if ($this->Event->save($this->data)) {
					$this->Session->setFlash(__('The event has been saved', true));
					$this->redirect(array('action' => 'index'));
				} else {
					$this->Session->setFlash(__('The event could not be saved. Please, try again.', true));
				}
			}
			if (empty($this->data)) {
				$this->data = $this->Event->read(null, $id);
			}
		} else {
			$this->Session->setFlash(__('Bu sayfaya erişmeye yetkiniz yoktur!', true));
			$this->redirect(array('controller'=>'pages','action' => 'index'));
		}
	}

	function delete($id = null) {
		$user = $this->Session->read('Auth.User');
		if($user['user_role']=='admin'){
			$this->layout = 'admin';
			if (!$id) {
				$this->Session->setFlash(__('Invalid id for event', true));
				$this->redirect(array('action'=>'index'));
			}
			if ($this->Event->delete($id)) {
				$this->Session->setFlash(__('Event deleted', true));
				$this->redirect(array('action'=>'index'));
			}
			$this->Session->setFlash(__('Event was not deleted', true));
			$this->redirect(array('action' => 'index'));
		} else {
			$this->Session->setFlash(__('Bu sayfaya erişmeye yetkiniz yoktur!', true));
			$this->redirect(array('controller'=>'pages','action' => 'index'));
		}
	}
	
	function beforeFilter() {
	    parent::beforeFilter();
	    $this->Auth->allow(array('*'));
    }
}
